$(function(){
	
	
	var jinzhi=0;
document.addEventListener("touchmove",function(e){
if(jinzhi==0){
e.preventDefault();
e.stopPropagation();
}
},false);



	var answer_arr=[];
	//预加载
	$.ajax({
		type:"get",
		url:"music.json",
		dataType:"json",
		success:function(res){
			var flag=true;
			res.sort(function(a,b){
				return Math.random() - 0.5;
			})
			console.log(res)
			
			var first_arr=[];
			var second_arr=[];
				music_load(first_arr);
			res.forEach(function(item,index){
				if(index<15){
					first_arr.push(item.audio);
				}else if(index<30){
					second_arr.push(item.audio);
				}
			})
			console.log("前十五首",first_arr);
			console.log("15-30首",second_arr);
			load1(first_arr,second_arr);
			var num=0;
		
			$("#begin_now").on("click",function(){
				$(".shouye").animate({left:'100%'}).fadeOut();
				$(".dati").css({display:"block"});
				time_f();
				var audio=document.querySelector("audio");
				audio.play();
				
				$("#selectA").html(res[0].item[0]);
				$("#selectB").html(res[0].item[1]);
				$("#selectC").html(res[0].item[2]);
				flag=false;
				var select_time=setInterval(function(){
					flag=true;
					if(flag==true){
						clearInterval(select_time)
					}
				},2000)
			
			chanage(first_arr,num);
			
			})
			var btn_music=document.querySelector(".audio");
		
			
			
			$(".select").on("click",function(e){
				
				$(".tips").removeClass("tishi");
						if(flag==true){
							
							flag=false;
							
							if($(this).index()==res[num].answer){
					answer_arr.push(1);
			
				}
				console.log(answer_arr);
					num++;
					$(".select").html("");
					$("#selectA").html(res[num].item[0]);
				$("#selectB").html(res[num].item[1]);
				$("#selectC").html(res[num].item[2]);
				
				if(num<15){
					chanage(first_arr,num);
				}else{
					num=num-15;
					chanage(second_arr,num);
				}
				var select_time=setInterval(function(){
					flag=true;
					if(flag==true){
						clearInterval(select_time)
					}
				},2000)
							
							
						}else{
							
							$(".tips").css({display:"block"});
							$(".tips").addClass("tishi");
						}
					
				
						
				
			
				
		
			})
			
			
		}
	});
	
	function load1(data1,data2){
		var queue=new createjs.LoadQueue();
		queue.loadManifest(data1);
		queue.on("progress",function(event){
			var progress=parseInt((event.progress*100).toFixed(2))+"%";
			$("#baifen").html(progress);
		},this);
		queue.on("complete",function(e){
			console.log("firstarr加载完成")
			$(".jiazai").fadeOut('fast');
			
			load2(data2);
		},this);
		queue.load();
	}
	
	function load2(data){
		var queue=new createjs.LoadQueue();
		queue.loadManifest(data);
		queue.on("complete",function(e){
			console.log("secondarr加载完成")
		});
		queue.load();
	}
	/*--------------------------------------------------------------------*/
	//开始游戏
	
	
	
	
	//定时器滑动
	function time_f(){
		var time=60;
		var stime=setInterval(function(){
			
			$(".clock").html(time+"s");
			console.log(time)
			if(time==50){
				$(".movice").addClass("xz_out");
				$(".movice").animate({left:"-100%",},500)
				$(".rock").animate({left:"-100%",},500)
			}else if(time==40){
				$(".rock").addClass("xz_out");
				$(".rock").animate({left:"-200%",},500)
				$(".jindian").animate({left:"-100%",},500)
			}else if(time==30){
				$(".jindian").addClass("xz_out");
				$(".jindian").animate({left:"-200%",},500)
				$(".che").animate({left:"-100%",},500)
			}else if(time==20){
				$(".che").addClass("xz_out");
				$(".che").animate({left:"-200%",},500)
				$(".old").animate({left:"-100%",},500)
			}else if(time==10){
				$(".old").addClass("xz_out");
				$(".old").animate({left:"-200%",},500)
				$(".liuxing").animate({left:"-100%",},500)
			}else if(time==0){
				$(".liuxing").animate({left:"-200%",},500)
				$(".dati").css({display:"none"});
				$(".audio").attr("src","");
				
				var rigth_num=answer_arr.length;
				if(rigth_num<=6){
					$("#levea_photo").attr("src","img/qingtong.png")
				}else if(rigth_num>6){
					$("#levea_photo").attr("src","img/baiying.png")
				}else if(rigth_num>12){
					$("#levea_photo").attr("src","img/huangjing.png")
				}else if(rigth_num>18){
					$("#levea_photo").attr("src","img/bojin.png")
				}else if(rigth_num>24){
					$("#levea_photo").attr("src","img/zhuangshi.png")
				}
				
				$("#howmuch_right").html("猜中  	"+answer_arr.length+" 首")
			}
		
			time--;
			
			
			
			if(time<0){
			clearInterval(stime);
		}
			
		},1000)
		
		
	};
	
	
	//首页音乐加载
	function music_load(arr,num){
		var audio=document.createElement("audio");
		var first_song;
		var song_src;
		
		for(var i=0;i<arr.length;i++){
			if(i==0){
				first_song=arr[i];
				audio.src=first_song;
			}
			
			
		}
		
	
		audio.loop="loop";
		audio.classList="audio";
		audio.id="aduio_id";
		audio.autoplay="autoplay";
		
			audio.addEventListener("canplay",function(){
				console.log("已完成");
				audio.play();
			})
			//error	发生错误时的回调
			audio.addEventListener("error",function(){
				console.log("发生错误");
			})
		document.body.appendChild(audio)
			
	}
	
	function chanage(arr,num){
		var first_song;
		var song_src;
		
		for(var i=0;i<arr.length;i++){
			
			if(i==num){
				song_src=arr[i];
			$(".audio").attr("src",song_src)
			}
			
		}
		
		
		
	}


		
	$("#pengyq").on("click",function(){
		
		$(".ydShare").css({display:"block"});
		
	})
	$(".ydShare").on("click",function(){
		$(".ydShare").css({display:"none"});
	})
	$("#agin").on("click",function(){
		window.location.reload()
	})
	
	
	
})
